using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Marlimer.Common.Entidades;
using Marlimer.Common;

namespace Marlimer.Data.Sistema
{
    public class sis_Sistema
    {
        public DataTable getAdminMenusPadreNiv1(int ccod_user, int ccod_appl)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsGetMenuPadresNiv1";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_user", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_appl", SqlDbType.Int);

            sqlcmd.Parameters["@ccod_user"].Value = ccod_user;
            sqlcmd.Parameters["@ccod_appl"].Value = ccod_appl;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public DataTable getAdminMenusHijos(int npadre, int nnivel, int ccod_user, int ccod_appl)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsGetMenuHijos";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion(); 

            sqlcmd.Parameters.Add("@npadre", SqlDbType.Int);
            sqlcmd.Parameters.Add("@nnivel", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_user", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_appl", SqlDbType.Int);

            sqlcmd.Parameters["@npadre"].Value = npadre;
            sqlcmd.Parameters["@nnivel"].Value = nnivel;
            sqlcmd.Parameters["@ccod_user"].Value = ccod_user;
            sqlcmd.Parameters["@ccod_appl"].Value = ccod_appl;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public resulMsgError doCrearMenus(List<InfoMenus> mnus)
        {
            resulMsgError resul = new resulMsgError(false, "");
 
            String comando = "spiaCrearMenus";
            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandText = comando;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.CommandType = CommandType.StoredProcedure;

            sqlcmd.Parameters.Add("@ccod_menu", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_appl", SqlDbType.Int);
            sqlcmd.Parameters.Add("@nindex", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnom_menu", SqlDbType.NVarChar, 50);
            sqlcmd.Parameters.Add("@nnivel", SqlDbType.Int);
            sqlcmd.Parameters.Add("@flg_padre", SqlDbType.Int);
            sqlcmd.Parameters.Add("@npadre", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cstatus", SqlDbType.NChar);
            sqlcmd.Parameters.Add("@cmodo", SqlDbType.Int);

            try
            {
                //modificar la tabla de dtClientes insertar
                foreach (InfoMenus mnu in mnus)
                {
                    sqlcmd.Parameters["@ccod_menu"].Value = mnu.ccod_menu;
                    sqlcmd.Parameters["@ccod_appl"].Value = mnu.ccod_appl;
                    sqlcmd.Parameters["@nindex"].Value = mnu.nindex;
                    sqlcmd.Parameters["@cnom_menu"].Value = mnu.cnom_menu;
                    sqlcmd.Parameters["@nnivel"].Value = mnu.nnivel;
                    sqlcmd.Parameters["@flg_padre"].Value = mnu.flg_padre;
                    sqlcmd.Parameters["@npadre"].Value = mnu.npadre;
                    sqlcmd.Parameters["@cstatus"].Value = mnu.cstatus;
                    sqlcmd.Parameters["@cmodo"].Value = mnu.cmodo;
                    sqlcmd.ExecuteNonQuery();
                }
                resul.resul = true;
            }
            catch (Exception ex)
            {
                resul.message = ex.Message;
            }

            return resul;
        }
    }
}
